package com.ebt.m.customer.util;

import android.text.TextUtils;

import com.ebt.m.customer.entity.Attachment;
import com.ebt.m.utils.fileutils.FileUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理保单附件的增删
 * Created by archer.qi on 2016/12/19.
 */
public class PolicyAttachOpt {

    public static boolean hasDeleted;
    // 最大允许的附件总数
    public static final int MAX_ATTACHMENT_ALLOW = 5;

    /**
     * 是否允许继续添加附件
     *
     * @param attachments
     * @return
     */
    public static boolean isAllowingToAdd(List<Attachment> attachments) {
        int total = getAttachmentCount(attachments);
        return total < MAX_ATTACHMENT_ALLOW;
    }

    /**
     * 获取有效的附件总数
     *
     * @return
     */
    public static int getAttachmentCount(List<Attachment> attachments) {
        if (attachments == null || attachments.size() == 0) {
            return 0;
        }
        int count = 0;
        for (Attachment attach :
                attachments) {
            if (attach != null && attach.option > -1) {
                count += 1;
            }
        }
        return count;
    }

    /**
     * 添加一个上传文件到附件集
     *
     * @param olds
     * @param newAttachPath
     * @return
     */
    public static List<Attachment> addAttach(List<Attachment> olds, String newAttachPath) {
        if (newAttachPath == null || "".equals(newAttachPath)) {
            return olds;
        }
        if (olds == null || (olds != null && olds.size() == 0)) {
            olds = new ArrayList<>();
        }
        Attachment attach = new Attachment();
        attach.setName(FileUtils.getFileName(newAttachPath));
        attach.setPath(newAttachPath);
        attach.option = 1;
        olds.add(attach);
        return olds;
    }

    /**
     * 添加一个删除附件标识
     * <br>1、服务器本来存在的附件，通过修改标识option = -1来统一删除<br/>
     * <br>2、本地临时添加的附件，直接移除附件集合，避免添加<br/>
     * <br>AliYun删除操作，只针对于已经存在于服务器的文件<br/>
     *
     * @param olds
     * @param pos
     * @return
     */
    public static List<Attachment> deleteAttach(List<Attachment> olds, int pos) {
        hasDeleted = false;
        if (olds == null) {
            return null;
        }
        if (olds.size() == 0) {
            return olds;
        }
        if (pos < 0 || pos > olds.size() - 1) {
            return olds;
        }
        int option = olds.get(pos).option;
        if (option == 0) {
            // 删除服务器上存在的附件，设置标志为-1
            olds.get(pos).option = -1;
            hasDeleted = true;
        }
        if (option == 1) {
            // 删除本地临时添加的附件，移出附件集合
            olds.remove(pos);
            hasDeleted = true;
        }
        return olds;
    }

    /**
     * 添加一个删除附件标识
     * <br>1、服务器本来存在的附件，通过修改标识option = -1来统一删除<br/>
     * <br>2、本地临时添加的附件，直接移除附件集合，避免添加<br/>
     * <br>AliYun删除操作，只针对于已经存在于服务器的文件<br/>
     *
     * @param olds
     * @param attach
     * @return
     */
    public static List<Attachment> deleteAttach(List<Attachment> olds, Attachment attach) {
        int pos = getPositionForDelete(olds, attach);
        return deleteAttach(olds, pos);
    }

    private static int getPositionForDelete(List<Attachment> olds, Attachment attach) {
        if (attach == null) {
            return -1;
        }
        if (olds == null || (olds != null && olds.size() == 0)) {
            return -1;
        }
        for (int i = 0; i < olds.size(); i++) {
            if (TextUtils.isEmpty(olds.get(i).getId())) {
                if (olds.get(i).getPath().equals(attach.getPath())) {
                    return i;
                }
            } else {
                if (olds.get(i).getId().equals(attach.getId())) {
                    return i;
                }
            }
        }
        return -1;
    }

}
